﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// DBLoginSet 的摘要说明
/// </summary>
public class DBLoginSetDAO
{
    SqlHelp help = new SqlHelp();
    private string sql = string.Empty;

    public DBLoginSetDAO()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public int AddDBLoginSet(DBLoginSetModel model)
    {
        SqlParameter[] paras = new SqlParameter[] {
                new SqlParameter("@DBCode",model.DBCode),
                new SqlParameter("@DBServerName",model.DBServerName),
                new SqlParameter("@DBServerIP",model.DBServerIP),
                new SqlParameter("@DBName",model.DBName),
                new SqlParameter("@DBLoginName", model.DBLoginName),
                new SqlParameter("@DBLoginPwd", model.DBLoginPwd),
                new SqlParameter("@DBType", model.DBType),
                new SqlParameter("@DBForFlag", model.DBForFlag),
                new SqlParameter("@DBDesc", model.DBDesc),
                new SqlParameter("@DeleteFlag", model.DeleteFlag),
                new SqlParameter("@AddAuthor", model.AddAuthor),
                new SqlParameter("@AddDate", model.AddDate),
                new SqlParameter("@UpdateAuthor", model.UpdateAuthor),
                new SqlParameter("@UpdateDate", model.UpdateDate)
              };
        sql = @"insert into B_DBLoginSet(DBCode,DBServerName,DBServerIP,DBName,DBLoginName,DBLoginPwd,DBType,
                DBForFlag,DBDesc,DeleteFlag,AddAuthor,AddDate,UpdateAuthor,UpdateDate)
            values(@DBCode,@DBServerName,@DBServerIP,@DBName,@DBLoginName,@DBLoginPwd,@DBType,@DBForFlag,@DBDesc,
                   @DeleteFlag,@AddAuthor,@AddDate,@UpdateAuthor,@UpdateDate)";

        return help.RunSqlInt(sql, paras);
    }

    public int EditDBLoginSet(DBLoginSetModel model)
    {
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@DBCode",model.DBCode),
            new SqlParameter("@DBServerName",model.DBServerName),
            new SqlParameter("@DBServerIP",model.DBServerIP),
            new SqlParameter("@DBName",model.DBName),
            new SqlParameter("@DBLoginName",model.DBLoginName),
            new SqlParameter("@DBLoginPwd",model.DBLoginPwd),
            new SqlParameter("@DBType",model.DBType),
            new SqlParameter("@DBDesc",model.DBDesc),
            new SqlParameter("@DBForFlag",model.DBForFlag),
            new SqlParameter("@UpdateAuthor",model.UpdateAuthor),
            new SqlParameter("@UpdateDate",model.UpdateDate)
        };
        sql = " update B_DBLoginSet set DBServerName=@DBServerName,DBServerIP=@DBServerIP,DBName=@DBName,DBLoginName=@DBLoginName,"
            + "DBLoginPwd=@DBLoginPwd,DBForFlag=@DBForFlag,DBType=@DBType,DBDesc=@DBDesc,UpdateAuthor=@UpdateAuthor,UpdateDate=@UpdateDate where DBCode=@DBCode";
        
        return help.RunSqlInt(sql, paras);
    }

    public int DeleteDBLoginSet(DBLoginSetModel model)
    {
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@DBCode",model.DBCode)
        };
        sql = " delete from B_DBLoginSet where DBCode=@DBCode ";

        return help.RunSqlInt(sql, paras);
    }

    public DataTable GetDBLoginSet()
    {
        sql = @" select DBCode,DBServerName,DBServerIP,DBName,DBLoginName,DBLoginPwd,DBDesc,isnull(DBType,0) DBType,
            case DBType when 0 then 'SQL Server' when 1 then 'DB2' else 'SQL Server' end DBTypeName,
            case DBForFlag when 1 then 'Trading' when 2 then 'Plant' else 'others' end DBForFlagName, DBForFlag, 
		    DeleteFlag,AddAuthor,AddDate,UpdateAuthor,UpdateDate
            from B_DBLoginSet where DeleteFlag=0 ";
        return help.GetDataTable(sql);
    }
}
